package com.hmjk.health.health;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hmjk.health.HealthApp;
import com.hmjk.health.R;
import com.hmjk.health.background.DataManager;
import com.hmjk.health.util.ADose;
import com.hmjk.health.util.ADrugReminder;
import com.hmjk.health.util.AReminder;
import com.hmjk.health.util.ReminderList;
import com.hmjk.health.util.reminderTime;
import com.hmjk.health.views.SwitchButton;
import com.hmjk.health.views.SwitchButton.OnChangeListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HealthDrugReminderActivity extends Activity implements OnClickListener{
	private final String TAG = HealthDrugReminderActivity.class.getSimpleName();
	private HealthApp mApp = null;
	
	private String tid = "";
	private ReminderList mReminderList = null;
	
	private LinearLayout mReminder = null;
	
	private void logd(String info){
		Log.d(TAG,info);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		logd("onCreate");
        super.onCreate(savedInstanceState);
        mApp = (HealthApp) getApplication();
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.health_drug_reminder_activity);
        
        ImageButton butBack = (ImageButton)findViewById(R.id.back);
		butBack.setOnClickListener(this);
		
		Intent i = getIntent();
        tid = i.getStringExtra("tid");
		
		mReminderList = new ReminderList();
		mReminder = (LinearLayout)findViewById(R.id.reminder_list);
		
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		retreiveReminderList();
		updateReminderList();
	}
	
	private void retreiveReminderList(){
		mReminderList.cleanReminder();
		
		String[] args = new String[1];
		args[0] = new String(tid);
		mReminderList = (ReminderList)mApp.getDataManager().checkData(DataManager.F_DRUG_REMINDER, args);
		//ADrugReminder rem = (ADrugReminder) mReminderList.getReminder(0);
		//logd("ADrugReminder, rem: " + rem.getDoseListJSONArrayStr());
	}
	
	private void updateReminderList(){
		int size = mReminderList.length();
		mReminder.removeAllViews();
		LayoutInflater inflater = LayoutInflater.from(this);
		if(size > 0){
			for(AReminder d:mReminderList.getReminderList()){
				ADrugReminder rem = (ADrugReminder)d;
				View v = inflater.inflate(R.layout.health_drug_reminder_item, null);
				LinearLayout itemView = (LinearLayout) v.findViewById(R.id.itemview);
				TextView drugName = (TextView)v.findViewById(R.id.drug_name);
				SwitchButton switchBut = (SwitchButton)v.findViewById(R.id.switch_but);
				TextView reminderTime = (TextView)v.findViewById(R.id.reminder_time);
				TextView reminderDay = (TextView)v.findViewById(R.id.reminder_day);
				ImageButton deleteBut = (ImageButton)v.findViewById(R.id.delete);
				
				reminderTime time = rem.time;
				String timeString = String.valueOf(time.Hour) + ":" + String.valueOf(time.Min); 
				reminderTime.setText(getResources().getString(R.string.reminder_time, timeString));
				String dayString = getResources().getString(R.string.reminder_everyday);
				if(time.repeat != 0xFF){
					dayString = "";
					for(int i=0; i<7; i++){
						if(((time.repeat >> i) & 0x01) == 1){
							if(dayString.isEmpty() == false){
								dayString = dayString + " " + " ";
							}
							switch(i){
							case 0:{
								dayString = dayString + getResources().getString(R.string.reminder_mon);
							}break;
							case 1:{
								dayString = dayString + getResources().getString(R.string.reminder_tue);
							}break;
							case 2:{
								dayString = dayString + getResources().getString(R.string.reminder_wed);
							}break;
							case 3:{
								dayString = dayString + getResources().getString(R.string.reminder_thur);
							}break;
							case 4:{
								dayString = dayString + getResources().getString(R.string.reminder_fri);
							}break;
							case 5:{
								dayString = dayString + getResources().getString(R.string.reminder_sat);
							}break;
							case 6:{
								dayString = dayString + getResources().getString(R.string.reminder_sun);
							}break;
							}
						}
					}
				}
				reminderDay.setText(dayString);
				//List<ADose> doseList = new ArrayList<ADose>();
				String doseListString = rem.getDoseListJSONArrayStr();
				String drugNameString = "";
				try {
					JSONArray arr = new JSONArray(doseListString);
					
					if(arr.length() > 0){
						//doseList.clear();
						for(int j = 0;j<arr.length();j++){
							JSONObject json = arr.getJSONObject(j);
							ADose dose = new ADose();
							dose.drugname = json.getString("drugname");
							dose.drugtype = json.getString("drugtype");
							dose.drugunit = json.getString("drugunit");
							dose.illness = json.getString("illness");
							Float f = new Float(json.getString("drugusage"));
							dose.drugusage = f.floatValue();
							if(drugNameString.isEmpty() == false){
								drugNameString = drugNameString + " " + " " + " ";
							}
							drugNameString = drugNameString + dose.drugname + " " + dose.drugusage + " " + dose.drugunit;
						}
					}
						
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				drugName.setText(drugNameString);
				if(rem.isopen == 1){
					switchBut.setFlage(true);
				}else {
					switchBut.setFlage(false);
				}
				itemView.setTag(rem);
				switchBut.setTag(rem);
				deleteBut.setTag(rem);
				
				itemView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ADrugReminder rem_data = (ADrugReminder)v.getTag();
						Intent i = new Intent(HealthDrugReminderActivity.this, HealthDrugReminderEdit.class);
						i.putExtra("tid", rem_data.tid);
						i.putExtra("index", rem_data.Index);
						i.putExtra("terminaltype", rem_data.devType);
						startActivity(i);
					}
				});
				switchBut.setOnChangeListener(new OnChangeListener() {
					
					@Override
					public void onChange(SwitchButton sb, boolean state) {
						ADrugReminder rem_data = (ADrugReminder)sb.getTag();
						if(state){
							rem_data.setState(1);
						}else {
							rem_data.setState(0);
						}
						//mApp.getDataManager().commitData(DataManager.F_DRUG_REMINDER, rem_data);
					}
				});
				deleteBut.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ADrugReminder rem_data = (ADrugReminder)v.getTag();
						rem_data.opt = 0xFF;
						//mApp.getDataManager().commitData(DataManager.F_DRUG_REMINDER, rem_data);
					}
				});
				
				mReminder.addView(v);
			}
		}
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.back:{
			finish();
		}break;
		
		case R.id.reminder_add_but:{
			
		}break;
		
		}
	}

}
